$(function() {
	// 展示资源树
	$("#tt").tree({
		url:path+'/resource/tree',
		animate:true,
		lines:true,
		onClick:function(node){
			if(node.id == 0){ // 说明是顶级节点
				addEvt();
				return;
			}
			editEvt();
			loadForm(node);
		},
		onLoadSuccess:function(node,data){
			addEvt();
			// 显示上级节点
			$("#res_pid").combotree({
				lines:true,
				animate:true,
				data:$("#tt").tree("getRoots")
			});
		}				
	});	
	
});
// 如果是添加
function addEvt(){
	$("#add_div").show();
	$("#edit_div").hide();
	$("#ff").form("reset");
	
	/*var url = path + "/resources/add/";
	$.post(url,function(data){
		createDiv(data);
	});*/
}
// 如果是修改 
function editEvt(){
	$("#add_div").hide();
	$("#edit_div").show();
}
// 修改页面加载表单 
function loadForm(node){
	var data = node.attrs;
	console.info(data);
	$("#ff").form("load",{
		"resource.id":data.id,
		"resource.res_name":data.res_name,
		"resource.res_pid":data.res_pid,
		"resource.res_url":data.res_url,
		"resource.res_name":data.res_name,
		"resource.res_icon":data.res_icon,
		"resource.res_remark":data.res_remark,
		"resource.menu_remark":data.menu_remark,
		"resource.res_order":data.res_order,
		"resource.res_target":data.res_target,
		"resource.status":data.status,
		"resource.auth_signs":(data.auth_signs).split(",")
	});
}

//点击添加按钮
function addM(){
	submitM(path+"/resource/save");
}

// 修改按钮
function editM(){
	submitM(path+"/resource/update");
}

// 重新添加
function reloadM(){
	window.location.reload();
}

// 删除按钮
function deleteM(){
	$.alert.confirm("确认删除记录？", function() {
		var url = path+"/resource/delete/"+$("#resource_id").val();
		window.location.href=url;
	});
}

function submitM(url){
	$.form.submit({
		url:url,
		success: function(result){
            // 返回有失败信息
            if (result.state == web_status.SUCCESS) {
                $("#tt").tree("reload");
            } else {
				$.alert.error(result.msg || "操作失败！");
            }
        }
	});
}

//点击全选和取消全选
function choose_all(){
	$("input[name='resources.auth_signs']").prop("checked",true);
}

function cancel_all(){
	$("input[name='resources.auth_signs']").removeAttr("checked");
}

// 辅助js
function createDiv(data){
	var div = "";
	$.each(data,function(index,value){
		if(value.is_checked){
			div += "<div class=\"col-xs-2\"><div class=\"checkbox\">"
				 +"<label for='signbox_"+value.id+"'>"
				 +"<input checked id='signbox_"+value.id+"' name=\"resources.auth_signs\" value='"+value.sign_name+"' type=\"checkbox\" class=\"ace\">"
				 +"<span class=\"lbl\">"+value.sign_remark+"</span></label></div></div>";
		}else{
			div += "<div class=\"col-xs-2\"><div class=\"checkbox\">"
				 +"<label for='signbox_"+value.id+"'>"
				 +"<input id='signbox_"+value.id+"' name=\"resources.auth_signs\" value='"+value.sign_name+"' type=\"checkbox\" class=\"ace\">"
				 +"<span class=\"lbl\">"+value.sign_remark+"</span></label></div></div>";
		}
	});
	$("#auth_signs_container").empty().append(div);
}